﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;
using ModelLayer;
using iTextSharp.text.html.simpleparser;
using BusinessLayer.Logic.UserLogic;
using DTO;
using System.Linq;
using System.Collections.ObjectModel;
namespace BusinessLayer.Logic.ReportProducer
{
   public class PdfCompetenzeProducer:IDisposable
    {
        Document document;
        MemoryStream ms;
        PdfWriter pw;

        public PdfCompetenzeProducer()
        {
            document = new Document();
            ms = new MemoryStream();
            pw=PdfWriter.GetInstance(document, ms);
            document.OpenDocument();
        }

        private void addList(Collection<Competenza> competenze, PdfPTable tableInner, Chunk domanda, Chunk risposta, Utente utente)
        {

           

            foreach (Competenza comp in competenze)
            {
                PdfPCell cell=new PdfPCell();
                Font fontDomanda = new Font(domanda.Font);
                Font fontRisposta = new Font(risposta.Font);
                String spazi="";
                for (int i = 0; i < (comp.Livello - 1); i++)
                {
                    spazi += " ";
                }
                Chunk domandaChunk=new Chunk(spazi+comp.Codice+" "+DictionaryManager.Instance.getString(utente.Compagnia.Lingua,comp.Codice),fontDomanda);
                String rispostaText = "";
                
                if (comp.Livello < 3)
                {
                    fontDomanda.SetStyle(Font.BOLD);
                    fontDomanda.Size += 3 - comp.Livello;
                }
                else
                {
                    AnagraficaPesi peso = comp.AnagraficaPesi.Where(x => x.Risposta.Count > 0).SingleOrDefault();
                    if (peso != null)
                    {
                        rispostaText = peso.AnagraficaRisposta.Nome;
                    }
                }
                
                
                
                Chunk rispostaChunk=new Chunk(rispostaText,fontRisposta);
                PdfPCell cell1=new PdfPCell();
                cell1.BorderWidth = 0;
                cell1.AddElement(domandaChunk);
                 PdfPCell cell2=new PdfPCell();
                 cell2.BorderWidth = 0;
                 cell2.AddElement(rispostaChunk);
                PdfPRow rowTable=new PdfPRow(new PdfPCell[]{cell1,cell2});
                
                tableInner.Rows.Add(rowTable);
                if (comp.Figli.Count>0)
                {
                    addList(comp.Figli, tableInner, domanda, risposta, utente);
                }
            }
            
        }

        public byte[] produce(Utente utente,String path)
        {

            ModelloLogic logic = new ModelloLogic();
            Modello modello=logic.getModelloConRisposteUtente(new DTOUtente() { Username = utente.Username });
           

            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

            System.Collections.Generic.Dictionary<string, object> providers = new System.Collections.Generic.Dictionary<string, object>();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            providers.Add(HTMLWorker.IMG_BASEURL, path);
            hw.SetProviders(providers);
            using (TextReader tx = File.OpenText(path+"/pdfReportCompetenze.htm"))
            {
                
                List<IElement> htmlarraylist =iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(tx, null,providers);
                IElement element = htmlarraylist[0];
                
                    PdfPTable table = (PdfPTable)element;

                    /* CREO IL TITOLO */
                    PdfPCell cellTitle = table.Rows[1].GetCells()[0];
                    Paragraph title = (Paragraph)cellTitle.CompositeElements[0];
                    Font fontTitle = title.Chunks[0].Font;
                    Chunk p1 = new Chunk("Risposte fornite da "+utente.Nome + " " + utente.Cognome+" all'assessment ECCO-EUCIP il "+DateTime.Now.ToShortDateString(),fontTitle); 
                
                    title.Clear();
                    title.Add(p1);

                    
                    

                  

                    PdfPCell cellTable = table.Rows[2].GetCells()[0];
                    PdfPTable tableRisposte = (PdfPTable)cellTable.CompositeElements[0];
                    Chunk domanda = ((Paragraph)tableRisposte.Rows[0].GetCells()[0].CompositeElements[0]).Chunks[0];
                    Chunk risposta = ((Paragraph)tableRisposte.Rows[0].GetCells()[1].CompositeElements[0]).Chunks[0];
                    tableRisposte.Rows.Clear();
                    table.SplitLate = false;
                    table.SplitRows = true;
                    addList(modello.Competenze,tableRisposte,domanda,risposta,utente);
                //    addList(table, info.competenzeInPiu, 10);

                    
                  
                    
                   

                    document.Add(element);
            }

           
           // document.Add(jpg);
            document.Close();
            return ms.ToArray();
        }



        public void Dispose()
        {
            
            document.Close();
            pw.Close();
            ms.Close();
        }
    }
}
